package com.zebraphp.devtool.util;

import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class IDate {

	public static final long ONE_MINUTE_MILLIONS = 60 * 1000;
	public static final long ONE_HOUR_MILLIONS = 60 * ONE_MINUTE_MILLIONS;
	public static final long ONE_DAY_MILLIONS = 24 * ONE_HOUR_MILLIONS;

//	public static int getDateFromTimeString(String datetime) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date date = null;
//		try {
//			date = sdf.parse(datetime);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
//		return calendar.get(Calendar.Y);
//	}

	public static String getDateFromTimeString(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);
	}

	public static int getHourFromTimeString(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinuteFromTimeString(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    //获取可读性 时间显示
	public static String getShortTime(String datetime){
		String parseStr = "";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Date curDate = new Date();

		long durTime = curDate.getTime() - date.getTime();
		int dayStatus = calculateDayStatus(date, curDate);

		if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
			parseStr = "刚刚";
		}else if (durTime <= 1* ONE_HOUR_MILLIONS) {
			parseStr = durTime / ONE_MINUTE_MILLIONS + "分钟前";
		}else if(dayStatus == 0 ){
			parseStr = durTime / ONE_HOUR_MILLIONS + "小时前";
		}else if(dayStatus == -1){//昨天
			parseStr = "昨天" + DateFormat.format("MM-dd", date).toString();
		}else if( isSameYear(date, curDate) && dayStatus < -1){
			parseStr = DateFormat.format("MM-dd", date).toString();
		}else{
			parseStr = DateFormat.format("yyyy-MM", date).toString();
		}

		return parseStr;
	}

	//获取可读性 时间显示
    public static String getShortTime(long curTimeStamp){
		String str = "";

		Date date = new Date(curTimeStamp*1000);
		Date curDate = new Date();
			
		long durTime = curDate.getTime() - date.getTime();
		int dayStatus = calculateDayStatus(date, curDate);

		if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
			str = "刚刚";
		}else if (durTime <= 1* ONE_HOUR_MILLIONS) {
			str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
		}else if(dayStatus == 0 ){
			str = durTime / ONE_HOUR_MILLIONS + "小时前";
		}else if(dayStatus == -1){//昨天
			str = "昨天" + DateFormat.format("MM-dd", date).toString();
		}else if( isSameYear(date, curDate) && dayStatus < -1){
			str = DateFormat.format("MM-dd", date).toString();
		}else{
			str = DateFormat.format("yyyy-MM", date).toString();
		}

		return str;
	}
	
	//计算相隔天数
	public static boolean isSameYear(Date targetTime, Date compareTime){
		Calendar tarCalendar = Calendar.getInstance();
		tarCalendar.setTime(targetTime);
		int tarYear = tarCalendar.get(Calendar.YEAR);
		
		Calendar compareCalendar = Calendar.getInstance();
		compareCalendar.setTime(compareTime);
		int comYear = compareCalendar.get(Calendar.YEAR);
		
		return tarYear ==comYear;
	}
	
	//计算相隔天数
	public static int calculateDayStatus(Date targetTime, Date compareTime){
		Calendar tarCalendar = Calendar.getInstance();
		tarCalendar.setTime(targetTime);
		int tarDayOfYear = tarCalendar.get(Calendar.DAY_OF_YEAR);
		
		Calendar compareCalendar = Calendar.getInstance();
		compareCalendar.setTime(compareTime);
		int comDayOfYear = compareCalendar.get(Calendar.DAY_OF_YEAR);
		
		return tarDayOfYear - comDayOfYear;
	}

	public static String StringToDate(String dateStr, String rawFormatDateStr, String toFormatDateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(rawFormatDateStr);
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat s = new SimpleDateFormat(toFormatDateStr);

		return s.format(date);
	}

}
